<?php
/**
 * Class 首页.
 * User: chan
 * Date: 2017/10/20 11:22
 */

namespace app\api\controller;

use think\Db;
use app\api\model\Show;
use app\api\model\Region;

class Index extends Base
{
    /**
    *   获取内容图文展示
    */
    public function getShow(){
        $input = input();
        if (empty($input['type'])){
            return $this->ajaxReturn('',-1,'未定义type');
        }
        $Show = new Show();
        $data = $Show->getAll(['type'=>$input['type']]);
        return $this->ajaxReturn($data);
    }

    /**
    *   获取内容图文展示
    */
    public function getLink(){
        $data = db('link')->select();
        return $this->ajaxReturn($data);
    }

    /**
    *   区域代理
    */
    public function getRegion(){
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        $Region = new Region();
        $data = $Region->getOne(['uid'=>$input['uid']]);
    
        if ($data) {
            $msg = $data['status'] == 1 ? '你已申请区域代理' : '你已成为区域代理';
            return $this->ajaxReturn([$data],200,$msg);
        }else {
            return $this->ajaxReturn('',-1,'未申请成为代理');
        }
    }
    /**
    *   添加区域代理
    */
    public function addRegion(){
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        if (empty($input['name'])){
            return $this->ajaxReturn('',-1,'未定义name');
        }
        if (empty($input['phone'])){
            return $this->ajaxReturn('',-1,'未定义phone');
        }
        // if (empty($input['province'])){
        //     return $this->ajaxReturn('',-1,'未定义province');
        // }
        // if (empty($input['city'])){
        //     return $this->ajaxReturn('',-1,'未定义city');
        // }
        // if (empty($input['county'])){
        //     return $this->ajaxReturn('',-1,'未定义county');
        // }
        $province = empty($input['province']) ? '' : $input['province'];
        $city = empty($input['city']) ? '' : $input['city'];
        $county = empty($input['county']) ? '' : $input['county'];
        $data = [];
        $Region = new Region();
        $tRegion = db('region')->where(['province'=>$province,'city'=>$city,'county'=>$county])->find();
        if ($tRegion) {
            return $this->ajaxReturn('',-1,'该区域已有代理');
        }else {
            $data['uid'] = $input['uid'];
            $data['name'] = $input['name'];
            $data['phone'] = $input['phone'];
            $data['province'] = $input['province'];
            $data['city'] = $input['city'];
            $data['county'] = $input['county'];
            $data['status'] = 1;
            $data['created_at'] = time();
            $id = $Region->add($data);
            if ($id) {
                $data['id'] = $id;
                return $this->ajaxReturn($data,200,'申请成功');
            }else {
                return $this->ajaxReturn('',-1,'申请失败');
            } 
        }
    }
    /**
     * 推广二维码
     */
    public function qrCode() {
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        vendor("phpqrcode.phpqrcode");
        $data = db('code')->where('uid',$input['uid'])->find();
        if (!$data) {
            $url = 'https://'.$_SERVER['SERVER_NAME'].'/index/Login/login?recommend_id=' . $input['uid']; //二维码内容 
            $outfile = ROOT_PATH . "public/qrcode/".time().'.jpg';
            $errorCorrectionLevel = 'L';//容错级别 
            $matrixPointSize = 6;//生成图片大小 
            $QRcode = new \QRcode();
            ob_start();
            //生成二维码图片 
            $QRcode->png($url, $outfile, $errorCorrectionLevel, $matrixPointSize, 2); 

            // $logo = ROOT_PATH . 'public/qrcode/1561020652.jpg';//准备好的logo图片 
            $logo = empty($input['logo']) ? FALSE : $input['logo'];
            $QR = $outfile;//已经生成的原始二维码图 

            if ($logo !== FALSE) { 

                $QR = imagecreatefromstring(file_get_contents($QR)); 

                $logo = imagecreatefromstring(file_get_contents($logo)); 

                $QR_width = imagesx($QR);//二维码图片宽度 

                $QR_height = imagesy($QR);//二维码图片高度 

                $logo_width = imagesx($logo);//logo图片宽度 

                $logo_height = imagesy($logo);//logo图片高度 

                $logo_qr_width = $QR_width / 5; 

                $scale = $logo_width/$logo_qr_width; 

                $logo_qr_height = $logo_height/$scale; 

                $from_width = ($QR_width - $logo_qr_width) / 2; 

                //重新组合图片并调整大小 

                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,  

                $logo_qr_height, $logo_width, $logo_height); 

            } 
            ob_end_clean();
            $data['uid'] = $input['uid'];
            $data['code_url'] = $outfile;
            $data['code_link'] = $url;
            $data['create_at'] = time();
            $id = db('code')->insertGetId($data);
            if ($id) {
                $data['id'] = $id;
            }else {
              return $this->ajaxReturn('',-1,'生成失败');  
            }
        }
        return $this->ajaxReturn($data);
    }
}